using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

namespace GameBaseFramework.Extension
{
    public static class ContainerExtension
    {
        #region 强制GC释放内存，只适合释放大型容器，小型容器交给GC.alloc()会更好
        /// <summary>
        /// 释放List的内存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        public static void ReleaseMemoryAndClear<T>(this List<T> self)
        {
            self.Clear();
            int identificador = GC.GetGeneration(self);
            GC.Collect(identificador, GCCollectionMode.Forced);
        }
        
        /// <summary>
        /// 释放Queue的内存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        public static void ReleaseMemoryAndClear<T>(this Queue<T> self)
        {
            self.Clear();
            int identificador = GC.GetGeneration(self);
            GC.Collect(identificador, GCCollectionMode.Forced);
        }

        /// <summary>
        /// 释放LinkedList的内存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        public static void ReleaseMemoryAndClear<T>(this LinkedList<T> self)
        {
            self.Clear();
            int identificador = GC.GetGeneration(self);
            GC.Collect(identificador, GCCollectionMode.Forced);
        }

        /// <summary>
        /// 释放HashSet的内存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        public static void ReleaseMemoryAndClear<T>(this HashSet<T> self)
        {
            self.Clear();
            HashSet<T> Temp = new();
            int identificador = GC.GetGeneration(self);
            GC.Collect(identificador, GCCollectionMode.Forced);
        }

        /// <summary>
        /// 释放Dictionary的内存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        public static void ReleaseMemoryAndClear<T, TR>(this Dictionary<T, TR> self)
        {
            self.Clear();
            int identificador = GC.GetGeneration(self);
            GC.Collect(identificador, GCCollectionMode.Forced);
        }
        #endregion

        /// <summary>
        /// 在双向链表里模拟入队
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        /// <param name="ListNode"></param>
        public static void Enqueue<T>(this LinkedList<T> self, T ListNode)
        {
            self.AddFirst(ListNode);
        }

        /// <summary>
        /// 在双向链表里模拟出队
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        /// <returns></returns>
        public static T Dequeue<T>(this LinkedList<T> self)
        {
            T Node = self.Last.Value;
            self.RemoveLast();
            return Node;
        }

        /// <summary>
        /// 销毁当前Node，并将Node置为下一个
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        /// <param name="Node"></param>
        public static void RemoveAndMoveNext<T>(this LinkedList<T> self, ref LinkedListNode<T> Node)
        {
            var temp = Node;
            Node = Node.Next;
            self.Remove(temp);
        }

        /// <summary>
        /// 为空或者无
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty<T>(this List<T> self)
        {

            return self == null || self.Count == 0;
        }

        /// <summary>
        /// 为空或者无
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty<T>(this LinkedList<T> self)
        {

            return self == null || self.Count == 0;
        }

    }
}
